<?php

namespace app\admin\controller\xykeep;

use app\common\controller\Backend;
use app\admin\model\xykeep\course\Order as CourseOrder;
use app\admin\model\xykeep\vip\Order as VipOrder;
use app\admin\model\xykeep\goods\Order as GoodsOrder;
use app\admin\model\xykeep\Appointment as AppointmentModel;
use app\admin\model\xykeep\user\Vip as VipModel;
use app\admin\model\xykeep\user\Withdraw as WithdrawModel;

/**
 * 控制台
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {

        //今日起始时间戳
        $beginToday = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
        $endToday = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y")) - 1;
        //昨日起始时间戳
        $beginYesterday = mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"));
        $endYesterday = mktime(0, 0, 0, date("m"), date("d"), date("Y")) - 1;
        //上周起始时间戳
        $beginLastweek = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"));
        $endLastweek = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"));
        //本月起始时间戳
        $beginThismonth = mktime(0, 0, 0, date("m"), 1, date("Y"));
        $endThismonth = mktime(23, 59, 59, date("m"), date("t"), date("Y"));

        //课程总销售额
        $courseOrderTotalMoney = CourseOrder::where(['status'=>1])->sum('total_fee');
        //课程总订单数
        $courseOrderTotalNum = CourseOrder::where(['status'=>1])->count();
        //课程今日销售额
        $courseOrderTodayMoney = CourseOrder::where(['status'=>1,'createtime' => ['between', [$beginToday, $endToday]]])->sum('total_fee');
        //课程今日订单数
        $courseOrderTodayNum = CourseOrder::where(['status'=>1,'createtime' => ['between', [$beginToday, $endToday]]])->count();
        //课程本月销售额
        $courseOrderMonthMoney = CourseOrder::where(['status'=>1,'createtime' => ['between', [$beginThismonth, $endThismonth]]])->sum('total_fee');
        //课程本月订单数
        $courseOrderMonthNum = CourseOrder::where(['status'=>1,'createtime' => ['between', [$beginThismonth, $endThismonth]]])->count();

        //会员卡总销售额
        $vipOrderTotalMoney = VipOrder::where(['status'=>1])->sum('total_fee');
        //会员卡总订单数
        $vipOrderTotalNum = VipOrder::where(['status'=>1])->count();
        //会员卡今日销售额
        $vipOrderTodayMoney = VipOrder::where(['status'=>1,'createtime' => ['between', [$beginToday, $endToday]]])->sum('total_fee');
        //会员卡今日订单数
        $vipOrderTodayNum = VipOrder::where(['status'=>1,'createtime' => ['between', [$beginToday, $endToday]]])->count();
        //会员卡本月销售额
        $vipOrderMonthMoney = VipOrder::where(['status'=>1,'createtime' => ['between', [$beginThismonth, $endThismonth]]])->sum('total_fee');
        //会员卡本月订单数
        $vipOrderMonthNum = VipOrder::where(['status'=>1,'createtime' => ['between', [$beginThismonth, $endThismonth]]])->count();

        //商品总销售额
        $goodsOrderTotalMoney = GoodsOrder::where(['status'=>1])->sum('total_fee');
        //商品总订单数
        $goodsOrderTotalNum = GoodsOrder::where(['status'=>1])->count();
        //商品今日销售额
        $goodsOrderTodayMoney = GoodsOrder::where(['status'=>1,'createtime' => ['between', [$beginToday, $endToday]]])->sum('total_fee');
        //商品今日订单数
        $goodsOrderTodayNum = GoodsOrder::where(['status'=>1,'createtime' => ['between', [$beginToday, $endToday]]])->count();
        //商品本月销售额
        $goodsOrderMonthMoney = GoodsOrder::where(['status'=>1,'createtime' => ['between', [$beginThismonth, $endThismonth]]])->sum('total_fee');
        //商品本月订单数
        $goodsOrderMonthNum = GoodsOrder::where(['status'=>1,'createtime' => ['between', [$beginThismonth, $endThismonth]]])->count();

        //待上课预约
        $appointmentNum = AppointmentModel::where(['status'=>0])->count();
        //待续费会员卡
        $vipDueNum = VipModel::where(['overdue_time'=>['<',time()]])->count();
        //待处理提现
        $withdrawNum = WithdrawModel::where(['status'=>['<',2]])->count();
        //待付款课程订单
        $dpayCourseOrder = CourseOrder::where(['status'=>0])->count();
        //待付款会员卡订单
        $dpayVipOrder = VipOrder::where(['status'=>0])->count();
        //待付款商品订单
        $dpayGoodsOrder = GoodsOrder::where(['status'=>0])->count();
        //待发货订单
        $ddGoodsOrder = GoodsOrder::where(['status'=>1])->count();


        $this->view->assign("courseOrderTotalMoney", $courseOrderTotalMoney);
        $this->view->assign("courseOrderTotalNum", $courseOrderTotalNum);
        $this->view->assign("courseOrderTodayMoney", $courseOrderTodayMoney);
        $this->view->assign("courseOrderTodayNum", $courseOrderTodayNum);
        $this->view->assign("courseOrderMonthMoney", $courseOrderMonthMoney);
        $this->view->assign("courseOrderMonthNum", $courseOrderMonthNum);

        $this->view->assign("vipOrderTotalMoney", $vipOrderTotalMoney);
        $this->view->assign("vipOrderTotalNum", $vipOrderTotalNum);
        $this->view->assign("vipOrderTodayMoney", $vipOrderTodayMoney);
        $this->view->assign("vipOrderTodayNum", $vipOrderTodayNum);
        $this->view->assign("vipOrderMonthMoney", $vipOrderMonthMoney);
        $this->view->assign("vipOrderMonthNum", $vipOrderMonthNum);

        $this->view->assign("goodsOrderTotalMoney", $goodsOrderTotalMoney);
        $this->view->assign("goodsOrderTotalNum", $goodsOrderTotalNum);
        $this->view->assign("goodsOrderTodayMoney", $goodsOrderTodayMoney);
        $this->view->assign("goodsOrderTodayNum", $goodsOrderTodayNum);
        $this->view->assign("goodsOrderMonthMoney", $goodsOrderMonthMoney);
        $this->view->assign("goodsOrderMonthNum", $goodsOrderMonthNum);

        $this->view->assign("appointmentNum", $appointmentNum);
        $this->view->assign("vipDueNum", $vipDueNum);
        $this->view->assign("withdrawNum", $withdrawNum);
        $this->view->assign("dpayCourseOrder", $dpayCourseOrder);
        $this->view->assign("dpayVipOrder", $dpayVipOrder);
        $this->view->assign("dpayGoodsOrder", $dpayGoodsOrder);
        $this->view->assign("ddGoodsOrder", $ddGoodsOrder);


        //日期
        $weekDays = xykeepGetWeeks();

        $courseDayTotalMoney = [];
        $courseDayTotalNum = [];
        $vipDayTotalMoney = [];
        $vipDayTotalNum = [];
        $goodsDayTotalMoney = [];
        $goodsDayTotalNum = [];
        foreach($weekDays as $wd){
            $courseDayTotalMoney[] = CourseOrder::where(['createtime'=>['between', [strtotime(date('Y').'-'.$wd), strtotime(date('Y').'-'.$wd)+86400]]])->sum('total_fee');
            $courseDayTotalNum[] = CourseOrder::where(['createtime'=>['between', [strtotime(date('Y').'-'.$wd), strtotime(date('Y').'-'.$wd)+86400]]])->count();

            $vipDayTotalMoney[] = VipOrder::where(['createtime'=>['between', [strtotime(date('Y').'-'.$wd), strtotime(date('Y').'-'.$wd)+86400]]])->sum('total_fee');
            $vipDayTotalNum[] = VipOrder::where(['createtime'=>['between', [strtotime(date('Y').'-'.$wd), strtotime(date('Y').'-'.$wd)+86400]]])->count();

            $goodsDayTotalMoney[] = CourseOrder::where(['createtime'=>['between', [strtotime(date('Y').'-'.$wd), strtotime(date('Y').'-'.$wd)+86400]]])->sum('total_fee');
            $goodsDayTotalNum[] = CourseOrder::where(['createtime'=>['between', [strtotime(date('Y').'-'.$wd), strtotime(date('Y').'-'.$wd)+86400]]])->count();
        }


        $this->assignconfig('weekDays',$weekDays);
        $this->assignconfig('courseDayTotalMoney',$courseDayTotalMoney);
        $this->assignconfig('courseDayTotalNum',$courseDayTotalNum);
        $this->assignconfig('vipDayTotalMoney',$vipDayTotalMoney);
        $this->assignconfig('vipDayTotalNum',$vipDayTotalNum);
        $this->assignconfig('goodsDayTotalMoney',$goodsDayTotalMoney);
        $this->assignconfig('goodsDayTotalNum',$goodsDayTotalNum);
        return $this->view->fetch();
    }


}
